/**
 * 
 */
package dms.presentation.action.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import dms.dto.BuildingDTO;
import dms.model.Building;
import dms.model.Campus;
import dms.model.util.PageIterator;
import dms.service.BuildingService;
import dms.service.CampusService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.PageUtil;

/**
 * @author leejohn
 *
 */
public class BuildingAction extends DispatchAction {
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		int[] params = null;
		params = PageUtil.parsePageParameter(request);
		int start = params[0];
		int count = params[1];
		
		BuildingService buildingService = (BuildingService) ContextUtil.getContext().getBean("buildingService");
		
		PageIterator<Building> buildings = buildingService.findAll(start, count);
		request.setAttribute("buildings", buildings);
		
		return mapping.findForward("list");
	}
	
	public ActionForward addForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CampusService campusService = (CampusService) ContextUtil.getContext().getBean("campusService");
		PageIterator<Campus> campuses = campusService.findAll(0, 0);
		
		request.setAttribute("campuses", campuses);
		return mapping.findForward("addForm");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		BuildingDTO dto = (BuildingDTO) form;
		
		BuildingService buildingService = (BuildingService) ContextUtil.getContext().getBean("buildingService");
		buildingService.createBuilding(dto);
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward updateForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {		
		
		Long buildingId = null;
		
		try {
			buildingId = Long.valueOf(request.getParameter("id"));
		} catch (NumberFormatException e) {
			log.error("id" + buildingId + "解析错误");
			return mapping.findForward(Constant.FAILURE);
		}
		BuildingService buildingService = (BuildingService) ContextUtil.getContext().getBean("buildingService");
		Building building = buildingService.findById(buildingId);
		CampusService campusService = (CampusService) ContextUtil.getContext().getBean("campusService");
		PageIterator<Campus> campuses = campusService.findAll(0, 0);
		request.setAttribute("building", building);
		request.setAttribute("campuses", campuses);
		
		return mapping.findForward("updateForm");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BuildingDTO dto = null;
		try {
			dto = (BuildingDTO) form;
			BuildingService buildingService = (BuildingService) ContextUtil.getContext().getBean("buildingService");
			buildingService.updateBuilding(dto);
			return mapping.findForward(Constant.SUCCESS);
		} catch (Exception e) {
			log.error("楼栋" + dto.getId() +"更新错误" + e.getMessage());
			return mapping.findForward(Constant.FAILURE);
		}
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long buildingId = null;
		
		try {
			buildingId = Long.valueOf(request.getParameter("id"));
			BuildingService buildingService = (BuildingService) ContextUtil.getContext().getBean("buildingService");
			buildingService.deleteBuildingById(buildingId);
			return mapping.findForward(Constant.SUCCESS);
			
		} catch (NumberFormatException e) {
			log.error("id" + buildingId + "解析错误");
			return mapping.findForward(Constant.FAILURE);
		} catch (Exception e) {
			log.error("没有这个Building ID");
			return mapping.findForward(Constant.FAILURE);
		}
		
	}
	
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		return this.list(mapping, form, request, response);
	}
}
